package com.ease.component.managers {

   import com.ease.component.controls.ErrorDialogEx;
   import com.ease.component.controls.errordialogclasses.ErrorListItem;

   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.sampler.getInvocationCount;

   import mx.collections.ArrayCollection;
   import mx.core.FlexGlobals;
   import mx.events.CloseEvent;
   import mx.events.FlexEvent;
   import mx.formatters.DateFormatter;
   import mx.logging.ILogger;
   import mx.managers.PopUpManager;

   public class ErrorManager {
      private static var _instance:ErrorManager = null;

      private var _errorCollection:ArrayCollection = new ArrayCollection();
      private var _popupErrDlg:ErrorDialogEx = null;

      public function ErrorManager(singletonToken:SingletonToken) {
      }

      /**
       * Static method, return an instance of ErrorManager
       * @return Return a global ErrorManager instance
       */
      public static function getInstance():ErrorManager {
         if(_instance == null) {
            _instance = new ErrorManager(new SingletonToken());
         }
         return _instance;
      }

      /**
       * Send error message to error dialog. If the error dialog has not been invoked,
       * popup the the error dialog. Otherwise, attach the error message into the error
       * dialog
       *
       * @param errorMessage
       * @param details
       */
      public static function showErrorMessage(errorMessage:String, details:String = null, error:Error = null):void {
         getInstance().showErrorMessageInternal(errorMessage, details, error);
		 trace(errorMessage);
      }

      private function showErrorMessageInternal(errorMessage:String, details:String, error:Error):void {
         // - Case: error message is empty
         if (("" == errorMessage || null == errorMessage) && null == error) {
            // - Can throw another common error message
            return;
         }

         if (_popupErrDlg == null) {
            _popupErrDlg = new ErrorDialogEx();
            _popupErrDlg.addEventListener(CloseEvent.CLOSE, closeHandler);
         }

         // The coming error message should be the first one if the error dialog has not
         // been popped up yet.
         if (!_popupErrDlg.isPopUp) {
            _errorCollection.removeAll();
         }

         var dateFormatter:DateFormatter = new DateFormatter();
         dateFormatter.formatString = "MM/DD/YYYY, L:NN:SS A"; //TODO wiwang: better place?
         // Related with ErrorDialogEx, every time just add one error message
         // Any change, please attention to ErrorDialogEx
         _errorCollection.addItemAt(
            new ErrorListItem(errorMessage, dateFormatter.format(new Date()), details, error)
            , 0); // Add new item to head

         _popupErrDlg.errors = _errorCollection;

         // If the error dialog had not been popped up yet, pop up it.
         if (!_popupErrDlg.isPopUp) {
            PopUpManager.addPopUp(_popupErrDlg, FlexGlobals.topLevelApplication.root, true);
            PopUpManager.centerPopUp(_popupErrDlg);
         }
      }

      private function closeHandler(event:Event):void {
         if (_popupErrDlg != null) {
            _popupErrDlg.removeEventListener(CloseEvent.CLOSE, closeHandler);
            _popupErrDlg = null;
         }
      }
   }
}

class SingletonToken {}
